﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ResourceManager
{
    class ConfigMgr
    {

    }

    /// <summary>
    /// 配置类
    /// </summary>
    public class Config
    {
        public static Config Instance
        {
            get
            {
                if (ms_instance == null)
                {
                    lock (ms_syncRoot)
                    {
                        if (ms_instance == null) ms_instance = new Config();
                    }
                }
                return ms_instance;
            }
        }

        /// <summary>
        /// Singleton instance
        /// </summary>
        protected static volatile Config ms_instance;

        /// <summary>
        /// Used for locking the instance calls
        /// </summary>
        protected static readonly object ms_syncRoot = new object();


        public bool Load()
        {
            if (_loaded)
            {
                return true;
            }
            try
            {
                _xmlRoot = XElement.Load(ConfigFile);
                _loaded = true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Failed to load " + ConfigFile + " : " + ex.Message);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 任务并发数
        /// </summary>
        public int MaxConcurrentTasksNumber
        {
            set
            {
                _MaxConcurrentTasksNumber = value;
            }
            get
            {
                if (_MaxConcurrentTasksNumber == null)
                {
                    Load();
                    _MaxConcurrentTasksNumber = int.Parse(_xmlRoot.Element("program").Element("connenct_limitation").Element("max_concurrent_tasks_number").Value);
                }
                return _MaxConcurrentTasksNumber.Value;
            }
        }

        /// <summary>
        /// HTTP下载超时
        /// </summary>
        public int Timeout
        {
            set
            {
                _Timeout = value;
            }
            get
            {
                if (_Timeout == null)
                {
                    Load();
                    _Timeout = int.Parse(_xmlRoot.Element("program").Element("connenct_limitation").Element("timeout").Value);
                }
                return _Timeout.Value;
            }
        }

        public string VersionFile
        {
            set
            {
                _VersionFile = value;
            }
            get
            {
                if (_VersionFile == null)
                {
                    Load();
                    _VersionFile = _xmlRoot.Element("remote").Element("version_file").Value;
                }
                return _VersionFile;
            }
        }
        private string _VersionFile;

        public string ConfigFile
        {
            set
            {
                _ConfigFile = value;
            }
            get 
            {
                if (_ConfigFile == null)
                {
                    _ConfigFile = DefaultResourceConfigFile;
                }
                return _ConfigFile;
            }
        }

        /// <summary>本地资源存放路径</summary>
        public string LocalPath 
        {
            set
            {
                _LocalPath = value;
            }
            get
            {
                if (_LocalPath == null)
                {
                    Load();
                    _LocalPath = _xmlRoot.Element("local").Element("path").Value;
                }
                return _LocalPath;
            }
        }

        /// <summary>远程资源存放路径</summary>
        public string RemotePath
        {
            set
            {
                _RemotePath = value;
            }
            get
            {
                if (_RemotePath == null)
                {
                    Load();
                    _RemotePath = _xmlRoot.Element("remote").Element("path").Value;
                }
                return _RemotePath;
            }
        }

        /// <summary>远程服务器IP地址</summary>
        public string RemoteIP
        {
            set
            {
                _RemoteIP = value;
            }
            get
            {
                if (_RemoteIP == null)
                {
                    Load();
                    _RemoteIP = _xmlRoot.Element("remote").Element("addr").Attribute("ip").Value;
                }
                return _RemoteIP;
            }
        }

        /// <summary>远程端口</summary>
        public int RemotePort
        {
            set
            {
                _RemotePort = value;
            }
            get
            {
                if (_RemotePort == null)
                {
                    Load();
                    _RemotePort = Int32.Parse(_xmlRoot.Element("remote").Element("addr").Attribute("port").Value);
                }
                return _RemotePort.Value;
            }
        }

        /// <summary>远程服务器类型</summary>
        public RemoteType RemoteType 
        {
            set
            {
                _RemoteType = value;
            }
            get
            {
                if (_RemoteType == null)
                {
                    Load();
                    _RemoteType = (RemoteType)Enum.Parse(typeof(RemoteType), _xmlRoot.Element("remote").Element("type").Value);
                }
                return _RemoteType.Value;
            }
        }


        private int? _RemotePort = null;
        private RemoteType? _RemoteType = null;
        private string _RemotePath = null;
        private string _RemoteIP = null;
        private string _LocalPath = null;
        private XElement _xmlRoot = null;
        private string _ConfigFile = null;
        private int? _Timeout = null;

        private const string DefaultResourceConfigFile = "./resource_config.xml";

        private int? _MaxConcurrentTasksNumber = null;
        private bool _loaded = false;
    }
}
